import Vue from "vue";
import store from '@/store'
import {
  Message
} from 'element-ui'
import VueRouter from "vue-router";
import {
  getToken
} from '@/utils/auth' // get token from cookie

Vue.use(VueRouter);

/* Layout */
import Layout from '@/layout'
import Business from '@/business'
import Person from '@/person'
import news from '@/views/news'


const routes = [{
    path: '/',
    component: Layout,
    redirect: 'work',
    children: [{ // 工作台
        path: '/work',
        name: 'work',
        component: () => import('@/views/work/index.vue'),
      },
      { // 经办合同
        path: '/handle',
        name: 'handle',
        component: () => import('@/views/handle/index.vue'),
      },
      { // 企业合同
        path: '/firm',
        name: 'firm',
        component: () => import('@/views/firm/index.vue'),
      },
      { // 模板管理
        path: '/template',
        name: 'template',
        component: () => import('@/views/template/index.vue'),
      },

      // 审批管理
      { // 合同审批
        path: '/pact',
        name: 'pact',
        component: () => import('@/views/verify/pact/index.vue'),
      },
      { // 用印审批
        path: '/seal',
        name: 'seal',
        component: () => import('@/views/verify/seal/index.vue'),
      },

      // 相对方管理
      { // 企业
        path: '/company',
        name: 'company',
        component: () => import('@/views/opp/company/index.vue'),
      },
      { // 个人
        path: '/unit',
        name: 'unit',
        component: () => import('@/views/opp/unit/index.vue'),
      },
      { // 企业详情
        path: '/companyDetail',
        name: 'companyDetail',
        component: () => import('@/views/opp/company/CompanyDetail.vue'),
      },
      { // 个人详情
        path: '/unitDetail',
        name: 'unitDetail',
        component: () => import('@/views/opp/unit/UnitDetail.vue'),
      },

      { // 证据管理
        path: '/proof',
        name: 'proof',
        redirect: 'proofSign',
        component: () => import('@/views/proof/index.vue'),
        children: [{ // 签署记录
            path: '/proofSign',
            name: 'proofSign',
            component: () => import('@/views/proof/ProofSign.vue'),
          },
          { // 签署详细
            path: '/proofSignDetail',
            name: 'proofSignDetail',
            component: () => import('@/views/proof/ProofSignDetail.vue'),
          },
          { // 出征记录
            path: '/proofGo',
            name: 'proofGo',
            component: () => import('@/views/proof/ProofGo.vue'),
          },
          { // 签署详细
            path: '/proofGoDetail',
            name: 'proofGoDetail',
            component: () => import('@/views/proof/ProofGoDetail.vue'),
          },
        ],
      },

      // 费用中心
      { // 订单
        path: '/order',
        name: 'order',
        redirect: 'myOrder',
        component: () => import('@/views/cost/order/index.vue'),
        children: [{ // 我的订单
            path: '/myOrder',
            name: 'myOrder',
            component: () => import('@/views/cost/order/myOrder.vue'),
          },
          { // 在线支付
            path: '/pay',
            name: 'pay',
            component: () => import('@/views/cost/order/pay.vue'),
          },
          { // 确认订单信息
            path: '/payInfo',
            name: 'payInfo',
            component: () => import('@/views/cost/order/payInfo.vue'),
          },
          { // 确认订单信息
            path: '/payResult',
            name: 'payResult',
            component: () => import('@/views/cost/order/payResult.vue'),
          },
        ]
      },
      { // 我的账单
        path: '/myAccount',
        name: 'myAccount',
        component: () => import('@/views/cost/account/index.vue'),
      },
      { // 查看账单
        path: '/lookAccount',
        name: 'lookAccount',
        component: () => import('@/views/cost/account/lookAccount.vue'),
      },


    ]
  },
  // 企业控制台
  { // 全部企业
    path: '/business/allCompany',
    name: 'allCompany',
    component: () => import('@/business/all.vue'),
  },
  {
    path: '/business',
    name: 'business',
    component: Business,
    redirect: '/business/companyInfo',
    children: [{ // 基本信息
        path: '/business/companyInfo',
        name: 'companyInfo',
        component: () => import('@/views/bloc/CompanyInfo.vue'),
      },
      { // 企业成员
        path: '/business/member',
        name: 'member',
        component: () => import('@/views/bloc/member.vue'),
      },
      { // 鸿签购买
        path: '/business/relevance',
        name: 'relevance',
        meta:{
          type:1,
        },
        component: () => import('@/views/bloc/relevance.vue'),
      },
      { // 确认订单信息
        path: '/business/contractPayInfo',
        name: 'contractPayInfo',
        component: () => import('@/views/cost/order/payInfo.vue'),
      },
      { // 印章管理
        path: '/business/sealAdminBusiness',
        name: 'sealAdminBusiness',
        component: () => import('@/views/bloc/SealAdminBusiness.vue'),
      },
      { // 新增企业印章
        path: '/business/addSeal',
        name: 'addSeal',
        component: () => import('@/views/bloc/AddSeal.vue'),
      },
      { // 查看授权列表
        path: '/business/impoweList',
        name: 'impoweList',
        component: () => import('@/views/bloc/ImpoweList.vue'),
      },
    ]
  },
  // 用户中心
  {
    path: '/person',
    name: 'person',
    component: Person,
    redirect: '/admin/userInfo',
    children: [{ // 信息管理
        path: '/admin/userInfo',
        name: 'userInfo',
        component: () => import('@/views/user/UserInfo.vue'),
      },
      { // 印章管理
        path: '/admin/sealAdmin',
        name: 'sealAdmin',
        component: () => import('@/views/user/SealAdmin.vue'),
      },
      { // 印章申请
        path: '/admin/addSealAdmin',
        name: 'addSealAdmin',
        component: () => import('@/views/user/AddSealAdmin.vue'),
      },
      { // 联系人管理
        path: '/admin/contactAdmin',
        name: 'contactAdmin',
        component: () => import('@/views/user/ContactAdmin.vue'),
      },
      { // 关于我们
        path: '/admin/aboutUs',
        name: 'aboutUs',
        component: () => import('@/views/user/AboutUs.vue'),
      },
      { // 账号设置
        path: '/admin/AccountSet',
        name: 'AccountSet',
        component: () => import('@/views/user/AccountSet.vue'),
      },
      { // 鸿签购买
        path: '/admin/relevanceAdmin',
        name: 'relevanceAdmin',
        meta:{
          type:0,
        },
        component: () => import('@/views/bloc/relevance.vue'),
      },
    ]
  },
  //  消息信息
   {
     path: '/news',
     name: 'news',
     component: news,
     redirect: '/news/newmessage',
     children: [
      { // 文件消息
         path: '/news/newmessage',
         name: 'newmessage',
         component: () => import('@/views/news/NewMessage.vue'),
       },
        { // 审批消息
          path: '/news/examinenews',
          name: 'examinenews',
          component: () => import('@/views/news/ExamineNews.vue'),
        },
        { // 系统消息
          path: '/news/setnews',
          name: 'setnews',
          component: () => import('@/views/news/SetNews.vue'),
        },
        { // 历史消息
          path: '/news/historynews',
          name: 'historynews',
          component: () => import('@/views/news/HistoryNews.vue'),
        },
     ]
   },

  // {
  //   path: "/about",
  //   name: "about",
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () =>
  //     import(/* webpackChunkName: "about" */ "../views/AboutView.vue"),
  // },
  {
    path: "/login",
    name: "login",
    component: () => import('@/views/login/index.vue')
  },
  {
    path: "/retrievePassword",
    name: "retrievePassword",
    component: () => import('@/views/login/retrievePassword.vue')
  },
  {
    path: "/retrieveAccount",
    name: "retrieveAccount",
    component: () => import('@/views/login/retrieveAccount.vue')
  },
  {
    path: "/realnameRetrieval",
    name: "realnameRetrieval",
    component: () => import('@/views/login/realnameRetrieval.vue')
  },
  { //直接发起经办合同
    path: '/directInitiation',
    name: 'directInitiation',
    component: () => import('@/views/handle/directInitiation.vue'),
  },
  {//直接发起
    path:'/initiate',
    name:'initiate',
    component:() =>import ('@/views/work/initiate.vue')
  },
  { //模板管理》新增模板
    path: '/newTemplate',
    name: 'newTemplate',
    component: () => import('@/views/template/newTemplate.vue'),
  },
  { //模板管理》新增模板》下一步
    path: '/setTemplate',
    name: 'setTemplate',
    component: () => import('@/views/template/setTemplate.vue'),
  },
  { //工作台》签署合同
    path: '/SignDocument',
    name: 'SignDocument',
    component: () => import('@/views/template/SignDocument.vue'),
  },
  { //个人认证
    path: '/PersonCertificat',
    name: 'PersonCertificat',
    component: () => import('@/views/work/PersonCertificat.vue'),
  },
  //   企业认证
  {
    path: '/authentication',
    name: 'authentication',
    component: () => import('@/views/authentication/index.vue'),
  }
];

const router = new VueRouter({
  routes,
});

const whiteList = ['/login','/retrieveAccount','/retrievePassword','/realnameRetrieval'] // no redirect whitelist

router.beforeEach(async (to, from, next) => {
  // start progress bar
  // NProgress.start()

  // set page title
  // document.title = getPageTitle(to.meta.title)

  // determine whether the user has logged in
  const hasToken = getToken()
// console.log(hasToken,"9999")
  if (hasToken) {
    if (to.path === '/login') {
      next({
        path: '/'
      })
      // NProgress.done()
    }
    else {
      const hasnickname = store.getters.nickname
      if (hasnickname) {
        next()
      } else {
        try {
          await store.dispatch('user/getUserInfo')
          // next({
          //   ...to,
          //   replace: true
          // })
          next()
        } catch (error) {
          // remove token and go to login page to re-login
          await store.dispatch('user/resetToken')
          Message.error(error || 'Has Error')
          next(`/login`)
        }
      }
    }
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next()
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      // next(`/login?redirect=${to.path}`)
      next(`/login`)
      // NProgress.done()
    }
  }
})

export default router;